package com.cout970.magneticraft.features

import com.cout970.magneticraft.AABB
import com.cout970.magneticraft.misc.CreativeTabMg
import com.cout970.magneticraft.misc.RegisterBlocks
import com.cout970.magneticraft.misc.block.get
import com.cout970.magneticraft.misc.resource
import com.cout970.magneticraft.misc.tileentity.getTile
import com.cout970.magneticraft.misc.vector.*
import com.cout970.magneticraft.misc.world.isClient
import com.cout970.magneticraft.systems.blocks.BlockBase
import com.cout970.magneticraft.systems.blocks.BlockBuilder
import com.cout970.magneticraft.systems.blocks.CommonMethods
import com.cout970.magneticraft.systems.blocks.IBlockMaker
import com.cout970.magneticraft.systems.itemblocks.itemBlockListOf
import com.cout970.magneticraft.systems.tilerenderers.PIXEL
import com.cout970.magneticraft.systems.tilerenderers.px
import net.minecraft.block.Block
import net.minecraft.block.material.Material
import net.minecraft.item.ItemBlock
import net.minecraft.util.EnumFacing
import net.minecraft.util.math.BlockPos
import net.minecraft.world.IBlockAccess

/**
 * Created by cout970 on 2017/08/10.
 */
@RegisterBlocks
object Blocks : IBlockMaker {
    lateinit var conveyorLvl0: BlockBase private set
    
    lateinit var inserterLvl0: BlockBase private set
    lateinit var inserterLvl1: BlockBase private set
    lateinit var inserterLvl2: BlockBase private set
    lateinit var inserterLvl3: BlockBase private set

    override fun initBlocks(): List<Pair<Block, ItemBlock>> {
        val builder = BlockBuilder().apply {
            material = Material.IRON
            creativeTab = CreativeTabMg
        }

        val conveyorBuilder = builder.copy {
            states = CommonMethods.Orientation.values().toList()
            hasCustomModel = true
            generateDefaultItemBlockModel = false
            alwaysDropDefault = true
            //methods
            boundingBox = { listOf(vec3Of(0, 0, 0) createAABBUsing vec3Of(1, 0.8125, 1)) }
            onBlockPlaced = CommonMethods::placeWithOrientation
            pickBlock = CommonMethods::pickDefaultBlock
            onActivated = CommonMethods::delegateToModule
        }

        val inserterBuilder = builder.copy {
            states = CommonMethods.Orientation.values().toList()
            hasCustomModel = true
            generateDefaultItemBlockModel = false
            alwaysDropDefault = true
            boundingBox = { listOf(vec3Of(0, 0, 0) createAABBUsing vec3Of(16.px, 3.px, 16.px)) }
            //methods
            onBlockPlaced = CommonMethods::placeWithOppositeOrientation
            pickBlock = CommonMethods::pickDefaultBlock
            onActivated = CommonMethods::openGui
        }

        conveyorLvl0 = conveyorBuilder.withName("conveyor_lvl0").copy {
            factory = factoryOf(::TileConveyorLvl0)
            customModels = listOf(
                "base" to resource("models/block/mcx/conveyor/lvl0/base.mcx"),
                "anim" to resource("models/block/mcx/conveyor/lvl0/anim.mcx"),
                "corner_base" to resource("models/block/mcx/conveyor/lvl0/corner_base.mcx"),
                "corner_anim" to resource("models/block/mcx/conveyor/lvl0/corner_anim.mcx"),
                "up_base" to resource("models/block/gltf/conveyor/lvl0/up_base.gltf"),
                "up_anim" to resource("models/block/gltf/conveyor/lvl0/up_anim.gltf"),
                "down_base" to resource("models/block/gltf/conveyor/lvl0/down_base.gltf"),
                "down_anim" to resource("models/block/gltf/conveyor/lvl0/down_anim.gltf"),
                "inventory" to resource("models/block/mcx/conveyor/lvl0/normal.mcx")
            )
        }.build()

        inserterLvl0 = inserterBuilder.withName("inserter_lvl0").copy {
            factory = factoryOf(::TileInserterLvl0)
            customModels = listOf(
                "model" to resource("models/block/gltf/inserter/lvl0/normal.gltf"),
                "inventory" to resource("models/block/gltf/inserter/lvl0/normal.gltf")
            )
        }.build()

        inserterLvl1 = inserterBuilder.withName("inserter_lvl1").copy {
            factory = factoryOf(::TileInserterLvl1)
            customModels = listOf(
                "model" to resource("models/block/gltf/inserter/lvl1/normal.gltf"),
                "inventory" to resource("models/block/gltf/inserter/lvl1/normal.gltf")
            )
        }.build()

        inserterLvl2 = inserterBuilder.withName("inserter_lvl2").copy {
            factory = factoryOf(::TileInserterLvl2)
            customModels = listOf(
                "model" to resource("models/block/gltf/inserter/lvl2/normal.gltf"),
                "inventory" to resource("models/block/gltf/inserter/lvl2/normal.gltf")
            )
        }.build()

        inserterLvl3 = inserterBuilder.withName("inserter_lvl3").copy {
            factory = factoryOf(::TileInserterLvl3)
            customModels = listOf(
                "model" to resource("models/block/gltf/inserter/lvl3/normal.gltf"),
                "inventory" to resource("models/block/gltf/inserter/lvl3/normal.gltf")
            )
        }.build()

        return itemBlockListOf(
            conveyorLvl0,
            inserterLvl0,
            inserterLvl1,
            inserterLvl2,
            inserterLvl3
        )
    }
}